package com.aionengine.gameserver.services.custom;

import com.aionemu.commons.services.CronService;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.services.abyss.AbyssPointsService;
import com.aionengine.gameserver.services.item.ItemService;
import com.aionengine.gameserver.services.teleport.TeleportService2;
import com.aionengine.gameserver.skillengine.model.SkillTargetSlot;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.world.InstanceType;
import com.aionengine.gameserver.world.World;
import com.aionengine.gameserver.world.WorldMap;
import com.aionengine.gameserver.world.WorldMapInstance;
import com.aionengine.gameserver.world.WorldMapInstanceFactory;
import com.aionengine.gameserver.world.WorldMapType;
import com.aionengine.gameserver.world.knownlist.Visitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author BlackCore
 */
public class PVPInstanceService{
    
	private static final WorldMapType PVP_INSTANCE_MAP = WorldMapType.UNDERGROUND_KATALAM;
	private static final int PVP_INSTANCE_ID = 99;
	
	private static final Logger log = LoggerFactory.getLogger(PVPInstanceService.class);
        
	 public static void startSpawnTask() {
		String time = "0 0 0/3 1/1 * ? *";
		CronService.getInstance().schedule(new Runnable() {
			@Override
			public void run() {
				if(pvpInstance().getPlayersInside().size()>=30){
					CustomSPAWN.spawnFarmMobsForPvp();
					broadcastMessage("Hiperion ha aparecido, matalo!",true);
					}
				else 
					broadcastMessage("Se necesita bastantes Guerreros en el JcJ para la aparicion de Hiperion!",true);			
			}

		}, time);
	}
         
	public synchronized static WorldMapInstance getPVPInstance() {
		WorldMap map = World.getInstance().getWorldMap(PVP_INSTANCE_MAP.getId());

		log.info("Creating new PVPinstance:" + PVP_INSTANCE_MAP + " id:" + PVP_INSTANCE_ID + " owner:" + 0);
		WorldMapInstance worldMapInstance = WorldMapInstanceFactory.createWorldMapInstance(map, PVP_INSTANCE_ID, 0, InstanceType.PVP);
		map.addInstance(PVP_INSTANCE_ID, worldMapInstance);
        startSpawnTask();
		return worldMapInstance;
	}
	
	public static WorldMapInstance pvpInstance() {
		return World.getInstance().getWorldMap(PVP_INSTANCE_MAP.getId()).getWorldMapInstanceById(PVP_INSTANCE_ID);
	}
        
       public static boolean isInPVP(Player player) {
		return player.getPosition().getWorldMapInstance().isPVPInstance();
	}
       public static void onKill(Player killer, Player victim) {
		if (isInPVP(killer)) {
			killer.increasePVPkills();
            onReward(killer);
        }
	}
       public static void onReward(Player killer) {
           int kills = killer.getPVPkills();
           switch(kills){
                   case 5:
                       AbyssPointsService.addAp(killer,1500); 
                       ItemService.addItem(killer, 186000031,1);
                       PacketSendUtility.sendMessage(killer,"[PVP] Una serie de asesinatos! Mataste " +kills+ " guerreros");
                       break;
                   case 10:
                       AbyssPointsService.addAp(killer, 3500);
                       ItemService.addItem(killer, 186000031,2);
                       PacketSendUtility.sendMessage(killer, "[PVP] Una serie de asesinatos! Mataste " +kills+ " guerreros");
                       break;
                   case 15:
                       AbyssPointsService.addAp(killer,5000);
                       ItemService.addItem(killer, 186000031,3);
                       broadcastMessage("[PVP] "+killer.getName()+ " hace una serie de " +kills+ " asesinatos",true);
                       killer.setPVPkills(0);
                       break;
                   default:
                       AbyssPointsService.addAp(killer, 100);
                       PacketSendUtility.sendMessage(killer, "[PVP] Tienes el premio estandar para matar");
                       break;
                             
                     
           }
       }
       private static void broadcastMessage(final String message, final boolean onCenter) {
		pvpInstance().doOnAllPlayers(new Visitor<Player>() {
			@Override
			public void visit(Player player) {
				if (onCenter)
					PacketSendUtility.sendBrightYellowMessageOnCenter(player, message);
				else
					PacketSendUtility.sendYellowMessage(player, message);	
			}
		});
	}
     
       
       public static void onEnter(Player p){
           if(isInPVP(p)&&!p.isInTeam()){
               PacketSendUtility.sendYellowMessage(p,"Se inicia la sesion en la zona como un jugador solitario!");
           }
       }
	
	
	public static void onLogOut(Player player) {
		if (isInPVP(player)) {
			TeleportService2.teleportToCapital(player);
		}
	}
	
	public static void onRevive(Player player) {
		Teleport.teleportToPVP(player);
		player.getKnownList().doUpdate();
		player.getEffectController().removeAbnormalEffectsByTargetSlot(SkillTargetSlot.DEBUFF);
	}
        
        
        public static WorldMapType getPVPInstanceMap() {
		return PVP_INSTANCE_MAP;
	}
	
	public static int getPVPInstanceId() {
		return PVP_INSTANCE_ID;
	}
    
}
